var gMyWidgets;

$(document).ready(function(){
	setLogin();
	getUserWidgetProfiles();
	$("#searchText").on('keydown', function(e) {
		if ( e.which == 13 ) {
			e.preventDefault();
			searchStore();
		}
	});
});



function getUserWidgetProfiles() {
	var u = getUser();
	if ( u == null ) {
		alert ("User must be logged in.");
		return;
	}
	var uri = store_rest_url + "/users/widgets";
	var authString = getBase64Authentication();
	$.ajax({
		type: "GET",
		url: uri,
		beforeSend: function(xhr) {
			xhr.setRequestHeader("Authorization", "Basic "+authString);
		},
		dataType: "text",
		success: displayUserWidgets
	});
}


function displayUserWidgets(jsonString) {
	var widgetList = $.parseJSON(jsonString);
	var html = buildWidgetLinks ( widgetList );
	$("#widget_list").html(html);
	var lastSelectedWidget = getCookie("lastSelectedWidget");
	selectWidget(lastSelectedWidget);
}


function buildWidgetLinks ( widgetList ) {
	var html = '';
	gMyWidgets = new Array();
	for (var i= 0; i < widgetList.length; i++ ) {
		var widget = widgetList[i];
		html += '<div class="widget_link" onclick="selectWidget(\''+widget.id+'\')" id="widgetid'+widget.id+'">';
		html += '<img src="'+widget.icon+'" width="16" height="16"/>';
		html += '&nbsp;<span class="widgetTitle">'+widget.name+'</span>';
		html += '</div>';
		gMyWidgets[widget.id] = widget;
	}
	html += '<div class="clear">&nbsp;</div>';
	return html;
}


function selectWidget ( widgetId) {
	if ( widgetId == null ) {
		$("#widget_title").html("&nbsp;");
		$("#widget_actions_panel").html("&nbsp;");
		return;
	}
	var widget = gMyWidgets[widgetId];
	var title = '<img src="'+widget.icon+'" widget="12" height="12">&nbsp;<span class="widgetTitle">'+widget.name+'</span>';
	$("#widget_title").html(title);
	$("#widget_actions_panel").html("");
	displayWidgetAnnotationsAction(widget);
	
	if ( typeof widget.functionalities != "undefined"  && widget.functionalities.length == 0 ) {
		displayFunctionalitiesAction(widget);
	}
	displayWidgetDeleteAction(widget);
	
	// set a cookie for future use
	setCookie ("lastSelectedWidget", widgetId, gStandardCookieTimeout);
	
	// set the highlight
	showSelection(widgetId);
}

function showSelection (widgetId) {
	for ( var i in gMyWidgets){
		var widget = gMyWidgets[i];
		if ( widget.id == widgetId ) {
			$("#widgetid"+widget.id).css("border","1px solid #DDDDDD");
		}
		else {
			$("#widgetid"+widget.id).css("border","1px solid #FFFFFF");
		}
	}
}



function displayWidgetAnnotationsAction(widget) {
	var html = '<div class="widget-action">You can tag, rate and review this widget on the widget view page. This allows the searching and ordering functions of the store to operate. <a href="widget_view.html?widgetId='+widget.id+'">Click Here</a></div>';
	$("#widget_actions_panel").append(html);
}


function displayFunctionalitiesAction(widget) {
	var html = '<div class="widget-action">There are no functionalites assigned to this widget.  Functionalities link a widget into iTec\'s recommendations engine the SDE. You can define functionalities for this widget <a href="widget_functionalities.html?widgetId='+widget.id+'&returnPage=user">here</a></div>';
	$("#widget_actions_panel").append(html);
}

function displayWidgetDeleteAction(widget) {
	var html = '<div class="widget-action"><div>You can delete this widget by clicking the button in this box.  This action cannot be undone.</div><div style="text-align:right"><button class="delete-widget-button" onClick="deleteWidget('+widget.id+')">Delete</button></div>';
	$("#widget_actions_panel").append(html);
}


function deleteWidget(widgetId) {
	var result = confirm("Are you sure you wish to delete this widget? This action cannot be undone!");
	if ( result ) {
		var u = getUser();
		if ( u == null ) {
			alert("You must be logged in to delete widgets");
			return;
		}
		var uri = store_rest_url+"/widgets/delete/"+widgetId;
		$.ajax({
			type:"DELETE",
			url:uri,
			beforeSend: function(xhr) {
				var authString = getBase64Authentication();
				xhr.setRequestHeader("Authorization", "Basic "+authString);
			},
			dataType:"text",
			success:widgetDeleted,
			error:widgetNotDeleted
		});
	}
}

function widgetDeleted(jsonString) {
	var msg = $.parseJSON(jsonString);
	if ( msg.message == "OK" ) {
		deleteCookie("lastSelectedWidget");
		getUserWidgetProfiles();
	}
	else {
		alert ("Unable to delete widget.  The message from the server is: "+msg.message);
	}
}

function widgetNotDeleted(jqXHR, textStatus, errorThrown ) {
	alert ("Unable to delete widget.  The message from the server is: "+jqXHR.responseText);
}